package leetcode_day._2021._202103;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 基本计算器 II
 * 算法：
 *   使用栈
 *   如果是空格，则直接跳过
 *   如果是加减号，则直接入栈
 *   如果是乘除，则直接把栈中的加减都算完
 *   3 * 2 / 2
 *   最后 while 循环栈，把加减给算了
 */
public class _11_227 {
    public static void main(String[] args) {
        System.out.println(new _11_227().calculate("1-1-1"));
    }

    public int calculate(String s) {
        s = s.trim();
        char[] cs = s.toCharArray();
        Deque<String> stack = new ArrayDeque<>();
        for (int i = 0; i < cs.length; i++) {
            if (cs[i] == ' ') {
                continue;
            }
            if (Character.isDigit(cs[i])) {
                StringBuilder tem = new StringBuilder();
                while (i < cs.length && Character.isDigit(cs[i])) {
                    tem.append(cs[i]);
                    ++i;
                }
                --i;
                int x = Integer.parseInt(tem.toString());
                if (stack.peek() != null) {
                    while (stack.peek() != null && (stack.peek().equals("*") || stack.peek().equals("/"))) {
                        char c = stack.peek().charAt(0);
                        stack.pop();
                        int a = Integer.parseInt(stack.pop());
                        if (c == '*') {
                            x = x * a;
                        } else {
                            x = a / x;
                        }
                    }
                }
                stack.push(x + "");
                continue;
            }
            stack.push(cs[i] + "");
        }
        while (stack.size() > 1) {
            int a = Integer.parseInt(stack.pop());
            char c = stack.pop().charAt(0);
            int b = Integer.parseInt(stack.pop());
            boolean flag = stack.peek() != null && stack.peek().equals("-");
            // 1-1-1
            if (stack.peek() == null && c == '+' || stack.peek() != null &&
                    (stack.peek().equals("-") && c == '-' || stack.peek().equals("+") && c == '+')) {
                b = b + a;
            } else if (stack.peek() == null && c == '-' || stack.peek() != null &&
                    (stack.peek().equals("+") && c == '-' || stack.peek().equals("-") && c == '+')) {
                b = b - a;
            }
            stack.push(b + "");
        }
        return Integer.parseInt(stack.pop());
    }

    public int calculate_perfect(String s) {
        Deque<Integer> stack = new ArrayDeque<>();
        int num = 0;
        char preSign = '+';
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i))) {
                num = num * 10 + (s.charAt(i) - '0');
            }
            if (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ' ' || i == s.length() - 1) {
                switch (preSign) {
                    case '+' : stack.push(num); break;
                    case '-' : stack.push(-num);break;
                    case '*' : stack.push(stack.pop() * num); break;
                    default: stack.push(stack.pop() / num);
                }
                preSign = s.charAt(i);
                num = 0;
            }
        }
        int ans = 0;
        while (!stack.isEmpty()) {
            ans += stack.pop();
        }
        return ans;
    }
}
